/**
 * 仿写JSON内置API的parse方法
 * @param {String} javaData JSON数据格式
 * @returns Object
 */
let javaData = '{"uname":"商品","price":18,"status":true}'
var s = parse(javaData)
console.log(s);
function parse(javaData){
    let webObj = {};
    javaData = javaData.replace(/{|}|"|\s/g,'');
    console.log(javaData);    //uname:商品,price:18,status:true
    javaData = javaData.split(',');
    console.log(javaData);    //['uname:商品', 'price:18', 'status:true']
    javaData.forEach(function(item,i){
        let arr = item.split(':')
        console.log(arr);      // ['uname', '商品']  ['price', '18']  ['status', 'true']
        let key = arr[0];
        let val = arr[1];
        if(isNaN(Number(val)) && val!='true' && val!='false'){
            webObj[key] = val;
            console.log(val,1);
        }
        if(!isNaN(Number(val)) && (val!='true' || val!='false')){
            webObj[key] = Number(val);
            console.log(val,2);
        }
        if(val=='true' || val=='false'){
            val = eval(val)    //将字符串"true",转换为 boolean型
            webObj[key] = val
            console.log(typeof val);
        }
    })
    console.log(webObj);     
    return webObj
}



/**
 * 仿写JSON内置API的stringify方法
 * @param {Object} obj 标准普通对象
 * @returns String
 */
 let webObj = {a:1,b:2,c:"漂亮",d:true}
 function stringify(webObj){
     let arr = []
     for(let key in webObj){
         if(webObj[key] == true || webObj[key] == false){
             let temp = `"${key}":${webObj[key]}`
              arr.push(temp)
         }
         else{
             let temp = `"${key}":"${webObj[key]}"`
             arr.push(temp)
         }
     }
     console.log(`{${arr.join(',')}}`); 
     return `{${arr.join(',')}}`
 }
 var s=stringify(webObj)